package com.yuanchu.media.controller;

import com.yuanchu.common.utils.R;
import com.yuanchu.media.entity.YoshopUploadFileEntity;
import com.yuanchu.media.service.YoshopUploadFileService;
import com.yuanchu.media.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-25 14:21
 */
@Slf4j
@RestController
// @RequestMapping("upload")
public class UploadFileController {


    @Resource
    private YoshopUploadFileService uploadFileService;


    @PostMapping("upload/image")
    public R uploadImage(HttpServletRequest httpServletRequest) throws IOException {
        Integer userId = SecurityUtil.getUserId();
        // 获取MultiPart请求
        MultipartHttpServletRequest multiPartRequest = (MultipartHttpServletRequest) httpServletRequest;
        // 获取所有上传的文件
        Map<String, MultipartFile> fileMap = multiPartRequest.getFileMap();
        MultipartFile file = fileMap.get("file");
        // 上传文件
        YoshopUploadFileEntity uploadFile = uploadFileService.uploadFileEntity(file, userId, 0, 20);
        Map<String, Object> data = new HashMap<>();
        data.put("fileInfo", uploadFile);
        return R.ok().put("data", data);
    }

    @PostMapping("/store/upload/image")
    public R uploadStoreImage(@RequestParam("iFile") MultipartFile file, @RequestParam("groupId") int groupId) throws IOException {
        Integer userId = SecurityUtil.getUserId();
        int channel = 10;
        if (groupId == -1){
            groupId = 0;
        }

        // 上传文件
        YoshopUploadFileEntity uploadFile = uploadFileService.uploadFileEntity(file, userId, groupId, channel);
        Map<String, Object> data = new HashMap<>();
        data.put("fileInfo", uploadFile);
        return R.ok().put("data", data);
    }

    @PostMapping("/store/upload/video")
    public R uploadStoreVideo(@RequestParam("iFile") MultipartFile file, @RequestParam("groupId") int groupId) throws IOException {
        Integer userId = SecurityUtil.getUserId();
        int channel = 10;
        if (groupId == -1){
            groupId = 0;
        }
        // 上传文件
        YoshopUploadFileEntity uploadFile = uploadFileService.uploadFileEntity(file, userId, groupId, channel);
        Map<String, Object> data = new HashMap<>();
        data.put("fileInfo", uploadFile);
        return R.ok().put("data", data);
    }

    @GetMapping("/file/info")
    public YoshopUploadFileEntity queryFiles(@RequestParam("fileId") Integer fileId){
        YoshopUploadFileEntity fileEntity = uploadFileService.getById(fileId);
        if (null == fileEntity){
            return null;
        }
        fileEntity.setPreviewUrl(fileEntity.getDomain() + fileEntity.getFilePath());
        fileEntity.setExternalUrl(fileEntity.getDomain() + fileEntity.getFilePath());
        return fileEntity;
    }

    /*@PostMapping("upload/image")
    public R uploadImage(HttpServletRequest httpServletRequest) throws IOException {
        Integer userId = SecurityUtil.getUserId();
        // 获取MultiPart请求
        MultipartHttpServletRequest multiPartRequest = (MultipartHttpServletRequest) httpServletRequest;

        // 获取所有上传的文件
        Map<String, MultipartFile> fileMap = multiPartRequest.getFileMap();
        MultipartFile multipartFile = fileMap.get("file");
        // 假设formDataIndex作为文件对应的表单项索引
        // List<String> fileIds = new ArrayList<>();
        YoshopUploadFileEntity uploadFile = null;
        for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
            String formDataIndex = entry.getKey(); // 可能需要解析key获取表单索引
            MultipartFile file = entry.getValue();
            // 构建接受请求类
            UploadFileParamsDto uploadFileParams = new UploadFileParamsDto();
            // 文件大小
            uploadFileParams.setFileSize(file.getSize());
            String contentType = file.getContentType();
            // 其他
            if (contentType != null && contentType.contains("image")) {
                // 图片
                uploadFileParams.setFileType(10);
            }
            // 文件名称
            uploadFileParams.setFilename(file.getOriginalFilename());
            // 文件类型
            uploadFileParams.setContentType(file.getContentType());
            // 文件字节数组
            byte[] bytes = null;
            try {
                bytes = file.getBytes();
            } catch (IOException e) {
                throw new YuanChuException("获取文件字节数组失败");
            }
            String folder = userId + "/";
            uploadFile = uploadFileService.uploadFile(userId, uploadFileParams, bytes, folder, null);
            log.info("upload image:{}", uploadFile);

            // 存储图片ID到列表
            // fileIds.add(fileId);
        }
        return R.ok().put("fileInfo", uploadFile);
    }*/

}
